(function ($) {
    $.fn.updateCountMess = function () {
        $.each($(this), function () {
            var thisLabel = $(this);
            var oldCount = parseInt(thisLabel.text());
            setInterval(function () {
                thisLabel.fadeOut(100);
                if (oldCount < 20) {
                    oldCount = oldCount + 1;
                }
                else {
                    oldCount = 20 + "+";
                }
                thisLabel.text(oldCount);
                thisLabel.fadeIn(100);
            }, 3000);
        });
    }
    $.fn.loadMesage = function (options) {
        var jsonNotice = [{ "title": "Thông báo từ Obama", "description": "Obama đã đặt hàng bạn trong mục Đồ Họa", "time": "9 phút trước", "link": "detailmessage.html", "read": "read" }, ];
        var defaults = { "jNote": jsonNotice, "panel": ".notice-panel" };
        var settings = $.extend(defaults, options || {});
        $.each($(this), function () {
            var thisClick = $(this);
            $(this).on("click", function () {
                thisClick.parent("li").find(settings.panel).find("ul").find("li").remove();
                thisClick.parent("li").find(settings.panel).find("ul").addClass("active").css({ "height": "3em" });
                var strJson = settings.jNote;
                var strHTML = "";
                setTimeout(function () {
                    thisClick.parent("li").find(settings.panel).find("ul").removeClass("active").css({ "height": "auto" });
                    for (var i = 0, len = strJson.length; i < len; i++) {
                        if (strJson[i].read = "read") {
                            strHTML += "<li>";
                        }
                        else {
                            strHTML += '<li class="no-read">';
                        }
                        strHTML += "<a href='" + strJson[i].link + "'>";
                        strHTML += "<p>" + strJson[i].title + "</p>";
                        strHTML += "<span>" + strJson[i].description + "</span>";
                        strHTML += "<label>" + strJson[i].time + "</label></a></li>";
                    }
                    thisClick.parent("li").find(settings.panel).find("ul").append(strHTML);
                    $(thisClick.parent("li").find(settings.panel).find("ul")).perfectScrollbar({
                        wheelSpeed: 20,
                        wheelPropagation: false,
                        suppressScrollX: true,
                        tagScroll: "li"
                    });
                }, 500);
            });
        });
    }
    $.fn.showDropDown = function (options) {
        var thisClick = $(this);
        var virtualArrow = $('<div/>', {
            "class": "top-arrow"
        });
        var defaults = { "showDiv": ".drop-down-profile", "setTopArrow": true, "childMenu": true };
        var settings = $.extend(defaults, options || {});
        if (settings.setTopArrow === true) {
            $(settings.showDiv).prepend(virtualArrow);
        }
        $(this).parent().find(settings.showDiv).slideUp(0);
        $(this).on("click", function (e) {
            e.preventDefault() ? e.preventDefault() : e.returnValue;
            $(settings.showDiv).slideUp(300, function () {
                thisClick.removeClass("active");
            });
            $(this).addClass("active");
            $(this).parent().children(settings.showDiv).slideDown(300);
        });
        $(document).on("click", function (e) {
            if (!$(e.target).closest(thisClick).length) {
                $(settings.showDiv).slideUp(300, function () {
                    thisClick.removeClass("active");
                });
            }
        });
        if (settings.childMenu) {
            $(settings.showDiv).find("li").on("mouseover", function () {
                $(this).find(">ul").addClass("active").show(500);
            });
            $(settings.showDiv).find("li").on("mouseleave", function () {
                $(this).find(">ul").removeClass("active").hide(500);
            });
        }
    }
    $.fn.kyersSplitButton = function (options) {
        var sliptButton = $(this);
        var aRrow = $("<input/>", { "type": "button", "class": "btn-split-arrow" });
        $(this).append(aRrow);
        var splitArrow = $(this).find(aRrow);
        var splitDropDown = $(this).find(".split-btn-dropdown");
        splitDropDown.slideUp(0);
        $(splitArrow).on("click", function () {
            splitDropDown.slideDown();
        });
        $(document).on("click", function (e) {
            if (!$(e.target).closest(sliptButton).length) {
                splitDropDown.slideUp(100);
            }
        });
    }
    $.fn.kyersTooltip = function (options) {
        var thisClick = $(this);
        var defaults = { "virTitle": "virtitle", "posArrow": "bottom" };
        var settings = $.extend(defaults, options || {});
        var virtualArrow = $('<div/>', {
            "class": "tooltip-arrow"
        });
        var toopTip = $('<div/>', {
            "class": "tooltip-wrap"
        });
        var toolTipBody = $('<div/>', {
            "class": "tooltip-body"
        });
        $(this).on("mouseover", function () {
            $(this).before(toopTip);
            $(this).parent().find(toopTip).prepend(virtualArrow);
            $(this).parent().find(toopTip).append(toolTipBody);
            $(this).parent().find(toolTipBody).text($(this).attr("virtitle"));
            $(this).parent().find(toolTipBody).show(0, function () {
                $(this).animate({ "min-height": "5em", "height": "auto" }, 300);
            });
        });
        $(this).on("mouseout", function () {
            $(this).parent().find(toolTipBody).hide(0, function () {
                $(this).animate({ "min-height": "0", "height": "auto" }, 0, function () {
                    $(toopTip).remove();
                });
            });
        });
    }
    $.fn.showFooter = function (options) {
        var thisClick = $(this);
        var defaults = { "footerBody": "#footer" };
        var virtualArrow = $('<div/>', {
            "class": "icon-arrow"
        });
        var settings = $.extend(defaults, options || {});
        $(this).append(virtualArrow);
        $(this).on("click", function () {
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
                $(settings.footerBody).removeClass("active");
            }
            else {
                $(this).addClass("active");
                $(settings.footerBody).addClass("active");
            }
        });
    }
    $(window).on("scroll", function () {
        if ($(document).scrollTop() >= $(".header-topbar").height()) {
            $(".header-topbar").addClass("active");
            var bdHeight = $(window).height() + $(".header-topbar").height();
            $("body").css("height", bdHeight);
        }
        else {
            $(".header-topbar").removeClass("active");
        }
    });
    $.convertTextToInt = function (options) {
        var newStr = "";
        for (var i = 0; i < options.length; i++) {
            if (!isNaN(parseFloat(options[i])) && isFinite(options[i])) {
                newStr += options[i];
            }
        }
        var newVal = 0;
        parseInt(newStr) ? newVal = parseInt(newStr) : newVal = 0;
        return parseInt(newVal);
    }
    $.fn.hightLightTable = function () {
        $.each($(this), function () {
            $(this).find("tbody").find("tr:odd").addClass("hight-light");
        });
    }
    //*DEMO JSON TOPIC STRING*//
    var jsonTopic = [{ "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê" }, { "idtopic": "1", "sprice": "50", "linkpost": "detailpost.html", "imgpost": "Evolution-Armchair-230x170.jpg", "title": "I will record nearly anything you want as", "description": "Donec sed odio dui. Nulla vitae elit libero, a pharetra augue", "userrank": "15", "userlink": "profile.html", "username": "Trung Lê"}];
    $.fn.showMoreContent = function (options) {
        var defaults = { "imgPath": "../Content/ImgResource/", "wrapContent": ".list-publish ul", "url": null };
        var settings = $.extend(defaults, options || {});
        $.each($(this), function () {
            $(this).on("click", function (e) {
                e.preventDefault() ? e.preventDefault() : e.returnValue;
                var htmlStr = "";
                var jsonString = "";
                if (settings.url === null) {
                    $.ajax({
                        type: "get",
                        //url: settings.url,
                        dataType: "text",
                        isLocal: true,
                        success: function (edata) {
                            //jsonString =$.parseJSON(edata.responseText);
                            jsonString = jsonTopic;
                            for (var i = 0, strlen = jsonString.length; i < strlen; i++) {
                                htmlStr += '<li><div class="publish-item"><div class="publish-thumb"> <label class="sprice-ticker">' + jsonString[i].sprice + 'K</label> <a href="' + jsonString[i].linkpost + '"><img src="' + settings.imgPath + jsonString[i].imgpost + '" alt="' + jsonString[i].imgpost + '"><span class="shadow-bg"></span> </a> <input class="btn" type="button" value="Giao dịch"> </div> <div class="publish-infor"><h3><a href="' + jsonString[i].linkpost + '">' + jsonString[i].title + '</a></h3> <p>' + jsonString[i].description + '</p> <div class="publish-description"> <span class="icon-count-rank">' + jsonString[i].userrank + '</span> <span class="icon-creater"><a href="' + jsonString[i].userlink + '">' + jsonString[i].username + '</a></span> </div> </div> </div></li>';
                            }
                            $(settings.wrapContent).after("<div class='loading-content'></div>");
                            setTimeout(function () {
                                $(".loading-content").remove();
                                $(settings.wrapContent).append(htmlStr);
                                $(".publish-item").showShadowBg();
                            }, 1000);
                        }
                    });
                }
                else {

                }
            });
        });
    }
    $.getObjectbyAttr = function (obj, key, val) {
        var virObj = [];
        for (var i in obj) {
            if (!obj.hasOwnProperty(i)) { continue };
            if (typeof obj[i] === 'object') {
                virObj = virObj.concat($.getObjectbyAttr(obj[i], key, val));
            } else if (i === key && obj[key] === val) {
                virObj.push(obj);
            }
        }
        return virObj;
    }

    // Validate Class
    String.prototype.validateNull = function () {
        var string = this.toString();
        if (string === null || string === "") {
            return false;
        }
        else {
            var vl;
            for (var i = 0; i < string.length; i++) {
                if (string.charCodeAt(i) !== 32) {
                    vl = true;
                }
                else {
                    vl = false;
                }
            }
            return vl;
        }
    }

    String.prototype.checkSameWord = function (edata) {
        var string = this.toString();
        if (string === edata)
            return true;
        return false;
    }

    String.prototype.validateNumPoint = function () {
        var string = this.toString();
        if (string.charCodeAt(0) === 46 || string.charCodeAt(string.length - 1) === 46) {
            return false;
        }
        else {
            var vl;
            if (string.length === 1) {
                if (isFinite(string) === true) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else {
                for (var i = 0; i < string.length; i++) {
                    if (string.charCodeAt(i) === 46 || isFinite(string.charAt(i)) === true) {
                        vl = true;
                    }
                    if (string.charCodeAt(i) !== 46 && isFinite(string.charAt(i)) === false) {
                        vl = false;
                    }
                }
            }
            return vl;
        }
    }
    String.prototype.validateEmail = function () {
        var string = this.toString();
        if (string.validateNull() === true) {
            var filter = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
            if (filter.test(string) === false) {
                return false;
            }
            else {
                return true;
            }
        }
        else {
            return false;
        }
    }
    $.fn.addTickerAlertInput = function (options) {
        var defaults = { "txtAlert": "Không được rỗng", "type": "checkNull", "controlToCompare": "#Password" };
        var settings = $.extend(defaults, options || {});
        $.each($(this), function () {
            $(this).on("focusout", function () {
                var postion = $(this).offset();

                var obgTicker = "<div class='alert-ticker'></div>";
                var objArrow = "<span class='alert-ticker-arrow'></span>";
                var thisTextBox = $(this);
                if (settings.type === "checkNull") {
                    if (thisTextBox.val().validateNull() === false && thisTextBox.siblings(".alert-ticker").length === 0) {
                        thisTextBox.before(obgTicker);
                        var ticker = thisTextBox.siblings(".alert-ticker");
                        $.setPos = function () {
                            thisTextBox.siblings(".alert-ticker");
                        }
                        ticker.text(settings.txtAlert);
                        ticker.append(objArrow);
                        $.setPos();
                    }
                    else if (thisTextBox.val().validateNull() === true) {
                        thisTextBox.siblings(".alert-ticker").remove();
                    }
                }
                if (settings.type === "checkEmail") {
                    if (thisTextBox.val().validateEmail() === false && thisTextBox.siblings(".alert-ticker").length === 0) {
                        thisTextBox.before(obgTicker);
                        var ticker = thisTextBox.siblings(".alert-ticker");
                        $.setPos = function () {
                            thisTextBox.siblings(".alert-ticker");
                        }
                        ticker.text(settings.txtAlert);
                        ticker.append(objArrow);
                        $.setPos();
                    }
                    else if (thisTextBox.val().validateEmail() === true) {
                        thisTextBox.siblings(".alert-ticker").remove();
                    }
                }
                if (settings.type === "checkSame") {
                    if (thisTextBox.val().checkSameWord($(settings.controlToCompare).val()) === false && thisTextBox.siblings(".alert-ticker").length === 0) {
                        thisTextBox.before(obgTicker);
                        var ticker = thisTextBox.siblings(".alert-ticker");
                        $.setPos = function () {
                            thisTextBox.siblings(".alert-ticker");
                        }
                        ticker.text(settings.txtAlert);
                        ticker.append(objArrow);
                        $.setPos();
                    }
                    else if (thisTextBox.val().checkSameWord($(settings.controlToCompare).text()) === true) {
                        thisTextBox.siblings(".alert-ticker").remove();
                    }
                }
            });
        });
    }
} (jQuery));

(function ($) {
    $(document).ready(function (e) {
        $(".account-name .user-name").showDropDown({ "showDiv": ".drop-down-profile" });
        $(".notify-icons-list .btn-icon").showDropDown({ "showDiv": ".notice-panel" });
        $(".dropdown-toggle").showDropDown({ "showDiv": ".category-list ul", "setTopArrow": false });
        var jsonNotice = [{ "title": "Thông báo từ Obama", "description": "Obama đã đặt hàng bạn trong mục Đồ Họa", "time": "9 phút trước", "link": "detailmessage.html", "read": "read" }, { "title": "Thông báo từ Obama", "description": "Obama đã đặt hàng bạn trong mục Đồ Họa", "time": "10 phút trước", "link": "detailmessage.html", "read": "no-read" }, { "title": "Thông báo từ Obama", "description": "Obama đã đặt hàng bạn trong mục Đồ Họa", "time": "10 phút trước", "link": "detailmessage.html", "read": "read" }, { "title": "Thông báo từ Obama", "description": "Obama đã đặt hàng bạn trong mục Đồ Họa", "time": "10 phút trước", "link": "detailmessage.html", "read": "no-read"}];

        var jsonMess = [{ "title": "Tin nhắn từ Obama", "description": "Bạn xem giúp mình cái đó, xấu vê lờ luôn, móa", "time": "9 phút trước", "link": "detailmessage.html", "read": "read" }, { "title": "Tin nhắn từ Obama", "description": "Bạn xem giúp mình cái đó, xấu vê lờ luôn, móa", "time": "9 phút trước", "link": "detailmessage.html", "read": "not-read"}];
        $("a.notify-icon").loadMesage({ "panel": ".notice-panel", "jNote": jsonNotice });
        $("a.message-icon").loadMesage({ "panel": ".notice-panel", "jNote": jsonMess });
        $(".footer-arrow").showFooter();
        $(".banner-icon").kyersTooltip();
        $(".split-button").kyersSplitButton();
        $("#gridTable").hightLightTable();
        $(".count-notice").updateCountMess();
        $("#txtSearchAuto").KyersAutoComplete();
    });

} (jQuery));